<?php
include 'includes.php';
include 'functions.php';
include 'config.php';
session_start();

$db = new db();
$cart = new Cart();

$list = $cart->get_all();

print_header("Mercatino dei Libri Usati", false);
print_sidebar_no_cart();

?>

<div id="main-content">

		
		
		<div id="book_list_container">
		<h1>Elenco Libri</h1>
		
		<div class="table_cart_1">Articoli</div>
		<div class="table_cart_3">Quantità</div>
		<hr style="margin:0">
		
		<div id="book_list">
		
		<?php
		if ( $cart->is_empty())
			echo "<p style='clear:both;'>Elenco vuoto</p>";
		else{
		
			foreach($list as $book):?>
			<form action="" method="post" accept-charset="utf-8" id="cart_form" onsubmit="return false;">			<div class="element" id="elem_<?=$book['id']?>">
				<div class="table_cart_1">
					<img alt="" src="<?php the_cover($book['id'], $book['EAN']) ?>">
					<p><?=$book['title'];?></p>
					<p style="font-size:10px; color:darkgray">di: <?=$book['author'];?></p>
					<a id="delete_<?=$book['id']?>" href="javascript:return false;" onclick="delete_element(this.id);">Elimina</a>
					<input type="hidden" id="book_id" value="<?=$book['id']?>">
				</div>

				<div class="table_cart_2">
					<input class="positive-integer" id="<?=$book['id']?>" name="<?=$book['id']?>" type="text" size=5 value="<?=$book['num_copies'];?>">
				</div>
				
			</div>
			<?php endforeach; ?>
		
		
		</form>
		<a href="javascript:return false;" id="empty_cart" class="button orange-button">Svuota Elenco</a>
		<a style="float:right" id="next" href="javascript:return false;" class="button orange-button">Prosegui</a>
		<?php }?>
		</div>
	
	</div>

	<div id="request_form">
	 <form id="book_send_request" name="book_send_request" method="post" action="" onsubmit="return false;">
      <fieldset id="send_request">
        <legend>Credenziali - tutti i campi sono obbligatori</legend>
          	
          	<label class="form_label" for="name">Nome</label>
          	<input type="text" name="name" id="name" class="required"/> 
          	
          	<label class="form_label" for="surname">Cognome</label>
          	<input type="text" name="surname" id="surname"  class="required"/>
          	
          	<label class="form_label" for="tax_code">Codice Fiscale</label>
          	<input type="text" name="tax_code" id="tax_code"  class="required"/>
 
          	<label class="form_label" for="city"> Localit&agrave;</label>
          	<input type="text" name="city" id="city"  class="required"/>
          	
          	<label class="form_label" for="address"> Indirizzo</label>
          	<input type="text" name="address" id="address"  class="required"/>
          	
          	<label class="form_label" for="cap"> CAP</label>
          	<input type="text" name="cap" id="cap"  class="required digits"/>
       
          	<label class="form_label" for="phone">Telefono</label>
          	<input type="text" name="phone" id="phone"  class="required"/>
       
          	<label class="form_label" for="mobile"> Cellulare</label>
          	<input type="text" name="mobile" id="mobile" class="required"/><span></span>
        
        	<label class="form_label" for="email">Email</label>
			<input type="text" name="email" id="email"  class="required email"/>
        
</fieldset>
      <br />
      
       <br />
       <fieldset>
        <legend>Altre comunicazioni o richieste:</legend>
          <p>
            <label for="comunicazioni"></label>
            <textarea name="additional" id="additional" cols="45" rows="5"></textarea>
          </p>
    </fieldset>
      <p>&nbsp;</p>
      
      
	<a id="book_send_request_submit" style="float:right" href="javascript:return false;" class="button orange-button">Invia richiesta</a>
     
</form>
	
	
	
	
	
	</div>
    
</div>

<script type="text/javascript">

var validation = $("#book_send_request").validate();

function delete_element(el_id){
	var del_book_id= $("#"+el_id).parent().children("#book_id").val();
	
	$.post("ajax.php", 
			{ request_type:"delete_book" , del_book_id : del_book_id },
			function(data) {
				if(data['deleted']){
					if (data['is_empty']){
						 $('#book_list').slideUp("slow" , function(){
							  $(this).html("<p>Elenco vuoto</p>");
							  $(this).slideDown("slow");		

						});
					}
					else $("#"+el_id).parents(".element").slideUp("slow");
					
						
				}


				},'json'


	 );
}

$("#next").click(function(){
	var dataSend = $("#cart_form").serialize()+"&request_type=finalize_cart"; 

	$.ajax({
		  url: 'ajax.php',
		  type: "POST",
		  data: dataSend,
		  dataType: "json",
		  success: function( data ) {
			  $("#book_list_container").slideUp("slow",function(){
					$("#request_form").slideDown("slow");
				});
			  }
	  	 
		  
		});
	
	
	
});

$("#empty_cart").click(function(){
	$.ajax({
		  url: 'ajax.php',
		  type: "POST",
		  data: {request_type: "empty_cart"},
		  dataType: "json",
		  success: function( data ) {
			  if( data['empty_cart'] == true){
				  $('#book_list').slideUp("slow" , function(){
				  $(this).html("<p>Elenco vuoto</p>");
				  $(this).slideDown("slow");
		    
		    	
		    });
			  }
		  }
		});
	
});

$("#book_send_request_submit").click(function(){
	
	$("#book_send_request").submit();
	
	
});

$("#book_send_request").submit(function(){
	if ( validation.form() ){
		trim_all();
		var dataSend = $("#book_send_request").serialize()+"&request_type=finalize_cart_2"; 
		$.ajax({
			  url: 'ajax.php',
			  type: "POST",
			  data: dataSend,
			  dataType: "json",
			  success: function( data ) {
				  $("#request_form").fadeOut("slow", function(){
					  $("#request_form").html("<h4 class='confirm'>Richiesta inoltrata correttamente, ti abbiamo inviato una email con la conferma dell'ordine.<h4>");
					  $('html, body').animate({ scrollTop: 0 }, 'slow');
					  $("#request_form").fadeIn("slow");


					  });
				  
			  }
			  
			});
	}
});


function trim_all(){
	$('input, textarea').each(function(){
	    $(this).val(jQuery.trim($(this).val()));
	});
	
}




$(".positive-integer").numeric({ decimal: false, negative: false }, function() { alert("Positive integers only"); this.value = ""; this.focus(); });
</script>


	
<?php 
print_footer();
?>